<?php


class ProductModelDetail extends Model{
    protected $_dbo;
    protected $_secid;
    protected $_catid;
    private $_property;
    var $sectionType;
    
	function __construct(){
        global $dbo;
	   $this->_dbo = $dbo;
       
		parent::__construct('com_product');
	}
	
	function action(){
		$this->setView('detail');
        $this->getIdByAliasSection();
        $this->getIdByAliasCategory();
         
        
        //assignTOView sectionalias
        $this->assignToView('section',$this->section);
        //assignTOView cateogryalias
        $this->assignToView('category',$this->category);
        
        $this->sectionType	= $this->getTypeSection($this->_secid);
        $this->assignToView('secType', $this->sectionType->type);
        
        
		if ($this->id){
			$this->assignToView('id', $this->id);
			$productDetails = $this->getProductDetail();
		    $this->assignToView('productDetails',$productDetails); 
		    $this->updateHitsProduct();
		}
        
		parent::action();
	}
	
	function getTitleCategory($calias){
		if($calias){
            $this->_dbo->setQuery("SELECT id,title FROM categories WHERE alias='$calias' AND published = 1 ");
           return $this->_dbo->loadObject();
        }
        return false;
	}
	
    function getIdByAliasSection(){
        if($this->section){
            $this->_dbo->setQuery("SELECT id FROM sections WHERE alias='$this->section' AND published = 1 ");
            $secid = $this->_dbo->loadObject();
            return $this->_secid = $secid->id;
        }
        return false;
    }
    
    function getIdByAliasCategory(){
        if($this->category){
            $this->_dbo->setQuery("SELECT id FROM categories WHERE alias='$this->category' AND published = 1 ");
            $catid = $this->_dbo->loadObject();
            return $this->_catid = $catid->id;
        }
        return false;
    }
    
    function getTypeSection($secid){
    	 $this->_dbo->setQuery("SELECT type FROM sections WHERE id= $secid ");
         return $this->_dbo->loadObject();
    }
    
    
    /****************Cac phuong thuc lay ve cac thong tin cua 1 san pham*******************/
    
    function getProductDetail(){
    	$query = " SELECT p.*
                       FROM products p
                       LEFT JOIN categories c ON p.category_id = c.id
                       RIGHT JOIN sections s ON c.section_id = s.id
                       WHERE p.trash = 0 
                            AND p.published = 1
                            AND c.published = 1
                            AND s.published = 1
                            AND p.id = $this->id
                     ";
        $this->_dbo->setQuery($query);
        return $this->_dbo->loadObjectList();
    }
    
    
    function getProductAttributeRelation($table,$properties,$id){
		$this->_dbo->setQuery("SELECT $properties FROM $table WHERE id = $id ");
		$results =  $this->_dbo->loadObjectList();
		foreach ($results AS $result){
			return $result->$properties;
		}
    }
    
    
	function getAttributeSimple($properties,$pid){
		$this->_property = $properties;
		$this->_dbo->setQuery("SELECT $this->_property FROM products WHERE id = $pid ");
		$results =  $this->_dbo->loadObject();
		if ($results->$properties == 1) echo 'Có';
		else echo 'Không';
    }
    
    
    function updateHitsProduct(){
    	$this->_dbo->setQuery("UPDATE products SET hits = hits+1 WHERE id = $this->id ");
    	return $this->_dbo->query();
    }   
    
}